package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.BuildingAdapter;
import com.sx.zongzhi.bean.BuildingBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.presenter.BuildingLayoutAddPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IBuildingLayoutView;
import com.sx.zongzhi.widget.LoadDialog;
import com.sx.zongzhi.widget.ScrollablePanel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 楼宇布局
 */
public class BuildingLayoutActivity extends BaseActivity implements IBuildingLayoutView{

    @BindView(R.id.scrollable_panel)
    ScrollablePanel scrollable_panel;
    private List<BuildingBean.BuildLayoutBean> buildingList=new ArrayList<>();
    private Disposable disposable;
    private String buildingId="";
    /**是否已有布局*/
    private boolean isLayout;
    private BuildingLayoutAddPest layoutAddPest;
    /**当前需要绑定的行列*/
    private BuildingBean.BuildLayoutBean currentBean;
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_building_layout);
        setTitleName("布局管理");
        ButterKnife.bind(this);
        layoutAddPest=new BuildingLayoutAddPest(this);
        buildingId=getIntent().getStringExtra("BuildingId");
        isLayout=getIntent().getBooleanExtra("Layout",false);
        initViews();
    }

    private void initViews() {
        if (isLayout){
            createEdit();
        }else {
            LoadDialog.show(BuildingLayoutActivity.this,"正在生成布局 请稍等");
            getLayout();
        }
        //订阅筛选组织机构
        disposable= MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent bean) throws Exception {
                        if ("BuildingLayoutActivity".equals(bean.getRequest())) {
                            if (bean.getType()==1) {//绑定返回
                                if (currentBean!=null){
                                    layoutAddPest.bindBuilding(buildingId, (String) bean.getMsg(),
                                            currentBean.getLayoutRow(),
                                            currentBean.getLayoutColumn());
                                }
                            }
                            if (bean.getType()==2){//解绑返回
                                getLayout();
                            }
                        }
                    }
                });

    }
    /**
     * 获取布局列表
     */
    private void getLayout() {
        layoutAddPest.loadBuildLayoutList(buildingId);
    }

    /**
     * 带输入框的dialog
     */
    private void createEdit() {
        View view= LayoutInflater.from(this).inflate(R.layout.dialog_building_edittext,null);
        final EditText et_Row= (EditText) view.findViewById(R.id.et_Row);
        final EditText et_column= (EditText) view.findViewById(R.id.et_column);
        Button btCancel=(Button) view.findViewById(R.id.dialog_back);
        Button btOk=(Button) view.findViewById(R.id.dialog_confirm);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.setView(view).setCancelable(false).create();
        dialog.show();
        btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
        btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String row=et_Row.getText().toString().trim();
                if (BaseUtil.isEmpty(row)||Integer.parseInt(row)<1||Integer.parseInt(row)>30){
                    ToastUtils.showToast(BuildingLayoutActivity.this,"层数应该在1-30");
                    return;
                }
                String column=et_column.getText().toString().trim();
                if (BaseUtil.isEmpty(column)||Integer.parseInt(column)<1||Integer.parseInt(column)>30){
                    ToastUtils.showToast(BuildingLayoutActivity.this,"户数应该在1-30");
                    return;
                }
                LoadDialog.show(BuildingLayoutActivity.this,"正在生成布局 请稍等");
                layoutAddPest.addBuildingLayout(buildingId,Integer.parseInt(row),Integer.parseInt(column));
                dialog.dismiss();
            }
        });
    }

    @Override
    public void addBuildingLayout(boolean success) {
        if (success){
           getLayout();
            MyRxBus.get().post(new MsgEvent<>("BuildingActivity",1,"添加布局成功"));
        }else {
            LoadDialog.dismiss(this);
            ToastUtils.showToast(this,"生成布局失败 请检查网络");
        }
    }

    @Override
    public void bindBuilding(boolean success) {
        if (success){
            ToastUtils.showToast(this,"绑定成功");
            getLayout();
        }
    }

    @Override
    public void loadBuildLayoutList(BuildingBean layoutBean) {
        LoadDialog.dismiss(this);
        if (layoutBean!=null&&layoutBean.getBuildLayout()!=null) {
            buildingList.clear();
            buildingList.addAll(layoutBean.getBuildLayout());
            BuildingAdapter adapter=new BuildingAdapter(this,buildingList,layoutBean.getTotalRow(),layoutBean.getTotalColumn());
            scrollable_panel.setPanelAdapter(adapter);
            adapter.setOnItemClickListener(new BuildingAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int rows,int column) {
                    for (int i=0;i<buildingList.size();i++){
                        BuildingBean.BuildLayoutBean buildLayoutBean=buildingList.get(i);
                        if (buildLayoutBean.getLayoutColumn()==column&&buildLayoutBean.getLayoutRow()==rows){
                            currentBean =buildingList.get(i);
                            break;
                        }
                    }
                    if (currentBean.isIsHouse()){//详情
                        startActivity(new Intent(BuildingLayoutActivity.this,BuildingHouseDetailActivity.class)
                            .putExtra("HouseId",currentBean.getHousePropertyId()));
                    }else {//选择房屋
                        startActivity(new Intent(BuildingLayoutActivity.this,
                                BuildingSelectHouseActivity.class));
                    }
                }
            });
        }else {
            ToastUtils.showToast(this,"生成布局失败 请检查网络");
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }



    @Override
    protected void onDestroy() {
        if (disposable!=null){
            disposable.dispose();
        }
        super.onDestroy();
    }
}
